package thu.cs.segroup.agent.behaviour;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import thu.cs.segroup.agent.Coordinator;
import thu.cs.segroup.entity.RunnerState;
import thu.cs.segroup.util.Constants;

import jade.core.Agent;
import jade.core.Location;
import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;

public class CoorDeployRunnerBehav extends OneShotBehaviour {

	private String runner;
	
	public CoorDeployRunnerBehav(String runner, Agent agent) {
		super(agent);
		this.runner = runner;
	}
	
	@Override
	public void action() {
		Coordinator coor = (Coordinator)myAgent;
		ArrayList<String> des = new ArrayList<String>();
		des.add(runner);
		HashMap<String, String> deploy_map = (HashMap<String, String>)coor.getProperty(Constants.COOR_RUNNER_DEPLOY_MAP);
		String host = deploy_map.get(runner);
		
		List<RunnerState> lst = coor.getRunners();
		for (int i = 0; i < lst.size(); i++) {
			if (lst.get(i).getName().equals(runner)) {
				lst.get(i).setState(Constants.STATE_RUNNER_BUSY);
				break;
			}
		}
		
		coor.getMainFrame().appendRunnerHostArea(runner, host);
		Location location = coor.getLocationByIP(host);
		// System.out.println("DeployRunner LOG: " + runner + " is deployed on" + host);
		// TODO
		myAgent.addBehaviour(new CommonSendObjMsgBehav(location, des, Constants.RUNNER_MIGRATE_MSG_EXT, ACLMessage.INFORM, myAgent));
	}

}
